package com.fone.player.activity.local;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.fone.player.R;
import com.fone.player.activity.BaseFragmentActivity;
import com.fone.player.activity.local.LocalFolderFragment;
import com.fone.player.activity.local.LocalFolderFragment.IFolderClickListener;
import com.fone.player.activity.local.LocalMediaFragment;
import com.fone.player.activity.local.LocalPrivateMediaFragment;
import com.fone.player.activity.local.SortListUtil;
import com.fone.player.activity.local.adapter.LocalMediaPagerAdapter;
import com.fone.player.entity.MediaFile;
import com.fone.player.entity.MediaFolder;
import com.fone.player.storage.StorageModule;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.util.ScreenUtil;
import com.fone.player.util.SystemUtil;
import com.fone.player.view.CustomDialog;
import com.fone.player.view.LocalViewPager;
import com.fone.player.view.PagerSlidingTab;
import com.fone.player.view.PagerSlidingTab.ITabsClickListener;

import de.greenrobot.event.EventBus;

/**
 * 
 * 本地视频
 * 
 * @author gjl
 * @since 2014-4-29
 */
@SuppressLint("HandlerLeak")
public class LocalActivity extends BaseFragmentActivity implements
		OnClickListener {

	private Context mContext;
	private String TAG = "LocalActivity";
	private TextView titleTextView;
	private Button leftBt, rightBt;

	private List<MediaFile> mVideos;
	private List<Fragment> mPagerViews;
	private List<MediaFolder> mFolders;
	private List<MediaFile> mPrivateVideos;
	private List<MediaFile> mFolderFiles;

	// 表示在按文件名排序、按添加时间排序、私人视频库、文件夹页面
	public static final int PAGE_MEDIA_NAME = 0, PAGE_MEDIA_TIME = 1,
			PAGE_FOLDER = 3, PAGE_PRIVATE_MEDIAS = 2;

	// 当前页面id
	public static int mCurrentPageID = PAGE_MEDIA_NAME;
	private int NameSortType = SortListUtil.SORT_NAME_ASC;
	private int TimeSortType = SortListUtil.SORT_DATE_ASC;
	public int PrivateTimeSortType = SortListUtil.SORT_DATE_DESC;
	private LocalViewPager mViewPager;
	private Dialog mProgressDialog;
	private LocalMediaFragment mNameFragment;

	private LocalMediaFragment mTimeFragment;

	private LocalPrivateMediaFragment mPrivateFragment;

	private LocalFolderFragment mFolderFragment;
	private PagerSlidingTab mLocalTabs;
	private boolean isFolderFileType;
	private View titleView;
	private Handler mHandler;
	private boolean hasEnterPrivateView;// 是否进入过私人视频页

	// handler id
	public final static int MSG_GET_MEDIA = 0;
	public final static int MSG_GET_ALL_MEDIA = 1;
	public final static int MSG_GET_MEDIA_FOLDER = 2;
	public final static int MSG_GET_MEDIA_FOLDER_FILE = 3;
	public final static int MSG_GET_PRIVATE_MEDIA = 4;
	public final static int MSG_SCROLL_VIEW_PAGER = 5;
	public final static int MSG_ENCRYPT_MEDIA = 6;// 加密
	public final static int MSG_DECODE_MEDIA = 7;// 解密
	public final static int MSG_DECODE_ALL_MEDIA = 8;// （私人库关闭时）全部解密
	public final static int MSG_DELETE_MEDIA = 9;// 删除视频

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.personal_local_video_view);
		mContext = getApplicationContext();
		initHandler();
		initView();
		// StorageModule.getInstance().copyDataBaseToSdCard();
	}

	private void initHandler() {
		EventBus.getDefault().register(this);
		mHandler = new Handler() {

			@Override
			public synchronized void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
				case MSG_GET_MEDIA:
					// 从媒体库扫描
					StorageModule.getInstance().getVideoFileList(
							StorageModule.LIST_TYPE_FILE_NORMAL_VIDEO);
					break;
				case MSG_GET_ALL_MEDIA:

					// 判断存储设备是否挂载
					if (SystemUtil.getInstance().isStorageMounted()) {
						// 全盘扫描
						StorageModule.getInstance().scanningAllStorage();

					} else {

						FoneUtil.showToast(mContext,
								R.string.no_sdcard_mounted_text);
					}

					break;
				case MSG_GET_MEDIA_FOLDER:
					// 获取文件夹
					StorageModule.getInstance().getVideoFolderList();
					break;
				case MSG_GET_PRIVATE_MEDIA:
					StorageModule.getInstance().getVideoFileList(
							StorageModule.LIST_TYPE_FILE_ENCRYPT_VIDEO);
					break;
				case MSG_GET_MEDIA_FOLDER_FILE:
					String path = (String) msg.obj;
					L.i(TAG, "Folder path:" + path);
					StorageModule.getInstance().getVideoFileListByFolderPath(
							path);
					break;
				case MSG_ENCRYPT_MEDIA:
					int EncryptPageId = msg.arg1;
					int EncryMediaPosition = msg.arg2;
					MediaFile mediaFile = null;
					if (EncryptPageId == PAGE_MEDIA_NAME
							|| EncryptPageId == PAGE_MEDIA_TIME) {
						mediaFile = mVideos.get(EncryMediaPosition);
					} else if (EncryptPageId == PAGE_FOLDER) {
						mediaFile = mFolderFiles.get(EncryMediaPosition);
					}
					if(mediaFile==null){
						return ;
					}
					L.i(TAG, "mediaFile:" + mediaFile.getMediaFileNameSuffix()
							+ "  id:" + mediaFile.getMediaFileId());
					int isSuccess = StorageModule.getInstance()
							.addEncryptFileByFileId(mediaFile.getMediaFileId());
					if (isSuccess == 1) {
						Toast.makeText(mContext, "该视频已经放入私人库",
								Toast.LENGTH_SHORT).show();

						if (EncryptPageId == PAGE_FOLDER) {
							mFolderFiles.remove(EncryMediaPosition);
							mFolderFragment
									.notifyData(LocalFolderFragment.FOLDER_FILE_MODE);
							StorageModule.getInstance().getVideoFolderList();
						}
						StorageModule.getInstance().getVideoFileList(
								StorageModule.LIST_TYPE_FILE_NORMAL_VIDEO);
						StorageModule.getInstance().getVideoFileList(
								StorageModule.LIST_TYPE_FILE_ENCRYPT_VIDEO);

					} else {
						Toast.makeText(mContext, "操作失败", Toast.LENGTH_SHORT)
								.show();
					}
					break;
				case MSG_SCROLL_VIEW_PAGER:
					mViewPager.setcanScroll(true);
					mViewPager.setOnTouchListener(null);
					int PageId = msg.arg1;
					int EncryptPosition = msg.arg2;
					int fromPageId = (Integer) msg.obj;
					if (EncryptPosition != -1) {// EncryptPosition！=-1时，表示进行加密
												// 不仅仅是切换页面
						mPrivateFragment.shouldScrollBcakPage = true;
						mPrivateFragment.shouldEncrptyMedia = true;
						mPrivateFragment.mEncrptyMediaPosition = EncryptPosition;
						mPrivateFragment.mFromPageId = fromPageId;
					}

					// 从文件夹模式的文件页面跳转过来
					if (fromPageId == PAGE_FOLDER
							&& mFolderFragment.MODE_TYPE == LocalFolderFragment.FOLDER_FILE_MODE) {
						mLocalTabs.setVisibility(View.VISIBLE);
						titleView.setVisibility(View.VISIBLE);
					} else if (PageId == PAGE_FOLDER //
							&& mFolderFragment.MODE_TYPE == LocalFolderFragment.FOLDER_FILE_MODE) {
						mLocalTabs.setVisibility(View.GONE);
						titleView.setVisibility(View.GONE);
						isFolderFileType = true;
					}
					mViewPager.setCurrentItem(PageId);
					break;
				case MSG_DECODE_MEDIA:
					int position = msg.arg1;
					if (mPrivateVideos != null) {
						long fileId = mPrivateVideos.get(position)
								.getMediaFileId();
						int result = StorageModule.getInstance()
								.deleteEncryptFileByFileId(fileId);
						if (result == 1) {
							Toast.makeText(mContext, "该视频已经移除私人库",
									Toast.LENGTH_SHORT).show();
							StorageModule.getInstance().getVideoFileList(
									StorageModule.LIST_TYPE_FILE_ENCRYPT_VIDEO);
							StorageModule.getInstance().getVideoFileList(

							StorageModule.LIST_TYPE_FILE_NORMAL_VIDEO);
							StorageModule.getInstance().getVideoFolderList();
						} else {
							Toast.makeText(mContext, "操作失败", Toast.LENGTH_SHORT)
									.show();
						}
					}
					break;
				case MSG_DECODE_ALL_MEDIA:
					// StorageModule.getInstance().getVideoFileList(eventBus,
					// StorageModule.LIST_TYPE_FILE_ENCRYPT_VIDEO);
					if (mPrivateVideos != null && mPrivateVideos.size() > 0) {
						for (MediaFile file : mPrivateVideos) {
							long id = file.getMediaFileId();
							int result = StorageModule.getInstance()
									.deleteEncryptFileByFileId(id);
							L.i(TAG,
									"mediaFile result:" + result
											+ "  file name:"
											+ file.getMediaFileNameSuffix());
						}
						StorageModule.getInstance().getVideoFileList(
								StorageModule.LIST_TYPE_FILE_ENCRYPT_VIDEO);
						StorageModule.getInstance().getVideoFileList(
								StorageModule.LIST_TYPE_FILE_NORMAL_VIDEO);
						StorageModule.getInstance().getVideoFolderList();
					}
					break;
				case MSG_DELETE_MEDIA:
					int MediaPosition = msg.arg1;
					int DeletePageId = msg.arg2;

					// 判断存储设备是否挂载
					if (SystemUtil.getInstance().isStorageMounted()) {
						
						// 删除
						deleteMedia(MediaPosition, DeletePageId);

					} else {

						FoneUtil.showToast(mContext,
								R.string.no_sdcard_mounted_text);
					}

					break;
				default:
					break;
				}
			}

		};
	}

	/**
	 * 
	 * 删除视频
	 * 
	 * @param MediaPosition
	 *            集合中的position
	 * @param DeletePageId
	 *            删除视频对应的页面
	 * @return void
	 * @throws
	 */
	public void deleteMedia(int MediaPosition, int DeletePageId) {
		String DeleteMediapath = null;
		switch (DeletePageId) {
		case PAGE_MEDIA_NAME:

		case PAGE_MEDIA_TIME:
			DeleteMediapath = mVideos.get(MediaPosition).getMediaFilePath();
			int result = StorageModule.getInstance().deleteFileByFilePath(
					DeleteMediapath);
			if (result == 1) {
				mVideos.remove(MediaPosition);
				mNameFragment.notifyData();
				mTimeFragment.notifyData();
				StorageModule.getInstance().getVideoFileList(
						StorageModule.LIST_TYPE_FILE_NORMAL_VIDEO);
				StorageModule.getInstance().getVideoFolderList();
			}
			L.i(TAG, "PAGE_MEDIA_NAME result:" + result);
			break;
		case PAGE_PRIVATE_MEDIAS:
			DeleteMediapath = mPrivateVideos.get(MediaPosition)
					.getMediaFilePath();
			int result1 = StorageModule.getInstance().deleteFileByFilePath(
					DeleteMediapath);
			if (result1 == 1) {
				mPrivateVideos.remove(MediaPosition);
				// StorageModule.getInstance().getVideoFileList(eventBus,
				// StorageModule.LIST_TYPE_FILE_NORMAL_VIDEO);
				StorageModule.getInstance().getVideoFileList(
						StorageModule.LIST_TYPE_FILE_ENCRYPT_VIDEO);
			}
			L.i(TAG, "PAGE_PRIVATE_MEDIAS result:" + result1);
			break;
		case PAGE_FOLDER:
			DeleteMediapath = mFolderFiles.get(MediaPosition)
					.getMediaFilePath();
			int result2 = StorageModule.getInstance().deleteFileByFilePath(
					DeleteMediapath);
			if (result2 == 1) {
				mFolderFiles.remove(MediaPosition);
				mFolderFragment
						.notifyData(LocalFolderFragment.FOLDER_FILE_MODE);
				StorageModule.getInstance().getVideoFolderList();
				StorageModule.getInstance().getVideoFileList(
						StorageModule.LIST_TYPE_FILE_NORMAL_VIDEO);

			}
			L.i(TAG, "PAGE_FOLDER result:" + result2);
			break;

		default:
			break;
		}

	}

	// 接受返回来的数据
	public void onEventMainThread(Message msg) {
		L.i(TAG, "msg.what:" + msg.what);
		switch (msg.what) {

		case StorageModule.MSG_ACTION_SCANNER_STARTED:
			L.i(TAG, " 扫描文件开始....");
			showScanProgressDialog();
			break;
		case StorageModule.MSG_ACTION_SCANNER_PROCESSING:
			// 扫描全盘文件进行中
			L.i(TAG, " 扫描全盘文件进行中....");
			break;
		case StorageModule.MSG_ACTION_SCANNER_FINISHED:
			// 扫描全盘文件结束
			dismissScanProgressDialog();
			StorageModule.getInstance().getVideoFileList(
					StorageModule.LIST_TYPE_FILE_NORMAL_VIDEO);
			StorageModule.getInstance().getVideoFolderList();
			break;
		case StorageModule.MSG_ACTION_SCANNER_STOPPED:
			// 扫描全盘文件停止 （没有执行完就停止）
			L.i(TAG, "扫描全盘文件停止 （没有执行完就停止）");
			break;
		case StorageModule.MSG_NORMAL_FILE_LIST_SUCCESS:
			// 普通文件全部列表返回成功
			L.i(TAG, "普通文件全部列表返回成功");
			Bundle VideoBundle = msg.getData();
			if (VideoBundle != null) {
				ArrayList<MediaFile> mMediaFileList = VideoBundle
						.getParcelableArrayList(MediaFile.MEDIA_FILE_LIST);
				mVideos.clear();
				mVideos.addAll(mMediaFileList);
				L.i(TAG, "mVideos.size:" + mVideos.size());
				if (mCurrentPageID == PAGE_MEDIA_NAME) {
					SortListUtil.sortList(mVideos, NameSortType, false);
				} else if (mCurrentPageID == PAGE_MEDIA_TIME) {
					SortListUtil.sortList(mVideos, TimeSortType, false);
				}
				if (mVideos.size() == 0) {
					rightBt.setOnClickListener(null);
					rightBt.setClickable(false);
					rightBt.setText("删除");
					rightBt.setTextColor(Color.GRAY);
				} else {
					rightBt.setOnClickListener(this);
					rightBt.setClickable(true);
					rightBt.setTextColor(getResources().getColor(
							R.color.local_btn_delete_color));
				}
				mNameFragment.notifyData();
				mTimeFragment.notifyData();
			}
			break;
		case StorageModule.MSG_NORMAL_FOLDER_LIST_SUCCESS:
			// 普通文件夹全部列表返回成功
			Bundle FolderBundle = msg.getData();
			ArrayList<MediaFolder> mMediaFolderList = FolderBundle
					.getParcelableArrayList(MediaFolder.MEDIA_FOLDER_LIST);
			mFolders.clear();
			mFolders.addAll(mMediaFolderList);
			// SortListUtil.sortList(mVideos, sortType, false);
			mFolderFragment.notifyData(LocalFolderFragment.FOLDER_MODE);
			L.i(TAG, "mMediaFolderList.size:" + mMediaFolderList.size());
			break;
		case StorageModule.MSG_FOLDER_FILE_LIST_SUCCESS:
			// 文件夹中文件全部列表返回成功
			Bundle FolderFileBundle = msg.getData();
			ArrayList<MediaFile> mMediaFolderFileList = FolderFileBundle
					.getParcelableArrayList(MediaFile.MEDIA_FILE_LIST);
			mFolderFiles.clear();
			mFolderFiles.addAll(mMediaFolderFileList);
			//
			SortListUtil.sortList(mFolderFiles, SortListUtil.SORT_NAME_ASC,
					false);
			L.i(TAG, "mFolderFiles size:" + mFolderFiles.size());
			mFolderFragment.notifyData(LocalFolderFragment.FOLDER_FILE_MODE);
			break;

		case StorageModule.MSG_ENCRYPT_FILE_LIST_SUCCESS:
			// 加密文件全部列表返回成功
			Bundle privateFileBundle = msg.getData();
			ArrayList<MediaFile> privateFileList = privateFileBundle
					.getParcelableArrayList(MediaFile.MEDIA_FILE_LIST);
			mPrivateVideos.clear();
			mPrivateVideos.addAll(privateFileList);
			SortListUtil.sortEncryptList(mPrivateVideos, PrivateTimeSortType);
			L.i(TAG, "mPrivateVideos size:" + mPrivateVideos.size());
			mPrivateFragment.notifyData(mPrivateVideos.size());
			break;
		default:
			break;
		}
	}

	private void initView() {
		initTitleView();
		if (mVideos == null) {
			mVideos = new ArrayList<MediaFile>();
		}
		mPrivateVideos = new ArrayList<MediaFile>();
		mFolders = new ArrayList<MediaFolder>();
		mFolderFiles = new ArrayList<MediaFile>();
		// 初始化pager中的view
		mViewPager = (LocalViewPager) findViewById(R.id.vp_local_video_file);
		mNameFragment = new LocalMediaFragment();
		mTimeFragment = new LocalMediaFragment();
		mNameFragment.setVideos(mVideos);
		mTimeFragment.setVideos(mVideos);
		mNameFragment.setHandler(mHandler);
		mTimeFragment.setHandler(mHandler);
		mPrivateFragment = new LocalPrivateMediaFragment();
		mPrivateFragment.setPrivateVideos(mPrivateVideos);
		mPrivateFragment.setLocativity(this);
		mPrivateFragment.setHandler(mHandler);
		mFolderFragment = new LocalFolderFragment();
		mFolderFragment.setFolderList(mFolders, mFolderFiles);
		mFolderFragment.setHandler(mHandler);
		mFolderFragment.setIFolderClickListener(new IFolderClickListener() {

			@Override
			public void showMedias() {
				mLocalTabs.setVisibility(View.GONE);
				titleView.setVisibility(View.GONE);
				// 当处于显示视频列表状态时，应禁止viewPager滑动
				mViewPager.setcanScroll(false);
				mViewPager.setOnTouchListener(new OnTouchListener() {
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						return true;

					}
				});
			}

			@Override
			public void showFolders() {
				mViewPager.setcanScroll(true);
				// mLocalTabs.setCanScroll(true);
				mLocalTabs.setVisibility(View.VISIBLE);
				titleView.setVisibility(View.VISIBLE);
				mLocalTabs.setCurrentPostion(PAGE_FOLDER);
				mViewPager.setOnTouchListener(null);
			}
		});
		mPagerViews = new ArrayList<Fragment>();
		mPagerViews.add(mNameFragment);
		mPagerViews.add(mTimeFragment);
		mPagerViews.add(mPrivateFragment);
		mPagerViews.add(mFolderFragment);

		LocalMediaPagerAdapter pagerAdapter = new LocalMediaPagerAdapter(
				this.getSupportFragmentManager(), mPagerViews);
		mViewPager.setAdapter(pagerAdapter);
		mLocalTabs.setTabPaddingLeftRight((ScreenUtil
				.getScreenWidthPix(mContext) - ScreenUtil.dp2px(15) * 14) / 8);
		mLocalTabs.setTypeface(null, 0);
		PageChangeListener listener = new PageChangeListener();
		mLocalTabs.setOnPageChangeListener(listener);
		mLocalTabs.setViewPager(mViewPager);
		int pageMargin = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 2, getResources()
						.getDisplayMetrics());
		mViewPager.setPageMargin(pageMargin);

	}

	// 头部初始化
	private void initTitleView() {
		titleView = findViewById(R.id.common_title_rl);
		leftBt = (Button) findViewById(R.id.common_title_left_bt);
		rightBt = (Button) findViewById(R.id.common_title_right_bt);
		titleTextView = (TextView) findViewById(R.id.common_title_text_tv);
		leftBt.setOnClickListener(this);
		rightBt.setOnClickListener(this);
		String LocalVideo = getResources().getString(R.string.local_title_back);
		String LocalDelete = getResources().getString(
				R.string.local_title_delete);
		leftBt.setText(LocalVideo);
		rightBt.setText(LocalDelete);
		titleTextView.setVisibility(View.GONE);
		rightBt.setOnClickListener(this);
		leftBt.setOnClickListener(this);
		mLocalTabs = (PagerSlidingTab) findViewById(R.id.local_tabs);
		mLocalTabs.setTextSize(ScreenUtil.dp2px(15));

		mLocalTabs.setTextColorResource(R.color.tab_unselect_color);
		mLocalTabs.setTextSelectedColorResource(R.color.tab_select_color);
		// 选中标签页底线
		mLocalTabs.setIndicatorColorResource(R.color.tab_select_color);
		mLocalTabs.setIndicatorHeight(3);
		// 标签页底线
		mLocalTabs.setUnderlineColorResource(R.color.tab_select_color);
		mLocalTabs.setUnderlineHeight(1);
		// 各标签间隔线
		mLocalTabs.setDividerColor(Color.TRANSPARENT);
		mLocalTabs.setTabsClickListener(new ITabsClickListener() {

			@Override
			public void ClickTab() {
				int pageID = mLocalTabs.getPageID();
				L.i(TAG, "pageid:" + pageID);
				L.i(TAG, "mcurrentId:" + mCurrentPageID);
				if (pageID == PAGE_MEDIA_NAME
						&& mCurrentPageID == PAGE_MEDIA_NAME) {
					if (NameSortType == SortListUtil.SORT_NAME_ASC) {
						NameSortType = SortListUtil.SORT_NAME_DESC;

					} else if (NameSortType == SortListUtil.SORT_NAME_DESC) {
						NameSortType = SortListUtil.SORT_NAME_ASC;
					}
					SortListUtil.sortList(mVideos, NameSortType, false);
					mNameFragment.notifyData();
				} else if (pageID == PAGE_MEDIA_TIME
						&& mCurrentPageID == PAGE_MEDIA_TIME) {
					if (TimeSortType == SortListUtil.SORT_DATE_ASC) {
						TimeSortType = SortListUtil.SORT_DATE_DESC;
					} else if (TimeSortType == SortListUtil.SORT_DATE_DESC) {
						TimeSortType = SortListUtil.SORT_DATE_ASC;
					}
					SortListUtil.sortList(mVideos, TimeSortType, false);
					mTimeFragment.notifyData();
				} else if (pageID == PAGE_PRIVATE_MEDIAS
						&& mCurrentPageID == PAGE_PRIVATE_MEDIAS) {
					if (mPrivateVideos.size() > 0) {
						if (PrivateTimeSortType == SortListUtil.SORT_DATE_ASC) {
							PrivateTimeSortType = SortListUtil.SORT_DATE_DESC;
						} else if (PrivateTimeSortType == SortListUtil.SORT_DATE_DESC) {
							PrivateTimeSortType = SortListUtil.SORT_DATE_ASC;
						}
						SortListUtil.sortEncryptList(mPrivateVideos,
								PrivateTimeSortType);
						mPrivateFragment.notifyData(mPrivateVideos.size());
					}

				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.common_title_left_bt:
			finish();
			break;
		case R.id.common_title_right_bt:
			if (mViewPager.getCurrentItem() == PAGE_MEDIA_NAME) {
				if (mNameFragment.getDeleted()) {
					mNameFragment.setDeleted(false);
					rightBt.setText("删除");
				} else {
					mNameFragment.setDeleted(true);
					rightBt.setText("取消");
				}
			} else if (mViewPager.getCurrentItem() == PAGE_MEDIA_TIME) {
				if (mTimeFragment.getDeleted()) {
					mTimeFragment.setDeleted(false);
					rightBt.setText("删除");
				} else {
					mTimeFragment.setDeleted(true);
					rightBt.setText("取消");
				}
			}
			break;

		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			switch (mCurrentPageID) {
			case PAGE_PRIVATE_MEDIAS:
				if (mPrivateFragment != null
						&& mPrivateFragment.getKeyBoardisVisible()) {
					mPrivateFragment.hideKeyBoardView();
					return true;
				}
				break;
			case PAGE_MEDIA_NAME:
				if (mNameFragment != null) {
					if (mNameFragment.getDeleted()) {
						mNameFragment.setDeleted(false);
						rightBt.setText("删除");
						return true;
					}
				}
				break;
			case PAGE_MEDIA_TIME:
				if (mTimeFragment != null) {
					if (mTimeFragment.getDeleted()) {
						mTimeFragment.setDeleted(false);
						rightBt.setText("删除");
						return true;
					}
				}
				break;
			case PAGE_FOLDER:
				if (mFolderFragment != null) {
					if (mFolderFragment.MODE_TYPE == LocalFolderFragment.FOLDER_FILE_MODE) {
						mFolderFragment
								.changeModeType(LocalFolderFragment.FOLDER_MODE);
						return true;
					}
				}
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);

	}

	// viewpager页面滑动监听
	private class PageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			L.i(TAG, "onPageScrollStateChanged arg0:" + arg0);
			if (arg0 == 0) {
				if (mFolderFragment.MODE_TYPE == LocalFolderFragment.FOLDER_FILE_MODE
						&& mViewPager.getCurrentItem() == PAGE_FOLDER) {
					mViewPager.setcanScroll(false);
					mViewPager.setOnTouchListener(new OnTouchListener() {
						@Override
						public boolean onTouch(View v, MotionEvent event) {
							return true;

						}
					});
				}
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			L.i(TAG, "onPageScrolled arg0:" + arg0);
		}

		@Override
		public void onPageSelected(int PageID) {
			L.i(TAG, "onPageSelected: pageid:" + PageID);
			switch (PageID) {
			case PAGE_MEDIA_NAME:
				// NameSortType = SortListUtil.SORT_NAME_ASC;
				rightBt.setVisibility(View.VISIBLE);
				mNameFragment.setDeleted(false);
				rightBt.setText("删除");
				SortListUtil.sortList(mVideos, NameSortType, false);
				mNameFragment.notifyData();
				mPrivateFragment.shouldEncrptyMedia = false;
				mPrivateFragment.shouldScrollBcakPage = false;
				mCurrentPageID = PageID;
				hasEnterPrivateView = false;
				break;
			case PAGE_MEDIA_TIME:
				// TimeSortType = SortListUtil.SORT_DATE_ASC;
				rightBt.setVisibility(View.VISIBLE);
				mTimeFragment.setDeleted(false);
				rightBt.setText("删除");
				SortListUtil.sortList(mVideos, TimeSortType, false);
				mTimeFragment.notifyData();
				mPrivateFragment.shouldEncrptyMedia = false;
				mPrivateFragment.shouldScrollBcakPage = false;
				mCurrentPageID = PageID;
				if (hasEnterPrivateView) {
					mPrivateFragment.initData();
				}
				break;
			case PAGE_PRIVATE_MEDIAS:
				rightBt.setVisibility(View.GONE);
				mPrivateFragment.initData();
				hasEnterPrivateView = true;
				// 获取加密视频
				StorageModule.getInstance().getVideoFileList(
						StorageModule.LIST_TYPE_FILE_ENCRYPT_VIDEO);
				// if (mCurrentPageID == PAGE_PRIVATE_MEDIAS) {
				// if (mPrivateVideos.size() > 0) {
				// SortListUtil.sortList(mPrivateVideos,
				// PrivateTimeSortType, false);
				// }
				// }

				mCurrentPageID = PageID;
				break;
			case PAGE_FOLDER:
				// if (mFolderFragment.MODE_TYPE ==
				// LocalFolderFragment.FOLDER_FILE_MODE
				// && mViewPager.getCurrentItem() == PAGE_FOLDER) {
				// mViewPager.setcanScroll(false);
				// // mLocalTabs.setCanScroll(false);
				// }
				mPrivateFragment.initData();
				rightBt.setVisibility(View.GONE);
				mPrivateFragment.shouldEncrptyMedia = false;
				mPrivateFragment.shouldScrollBcakPage = false;
				mCurrentPageID = PageID;
				if (!isFolderFileType) {
					mFolderFragment
							.changeModeType(LocalFolderFragment.FOLDER_MODE);
				} else {
					isFolderFileType = false;
				}
				break;

			default:
				break;
			}
		}
	}

	// 展示全盘扫描时的对话框
	public void showScanProgressDialog() {
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
			mProgressDialog = null;
		}

		View DialogView = LayoutInflater.from(mContext).inflate(
				R.layout.scan_media_dialog_view, null);
		mProgressDialog = new CustomDialog(this);
		mProgressDialog.setContentView(DialogView, new LayoutParams(
				(int) (ScreenUtil.getScreenWidthPix(mContext) * 0.8),
				(int) (ScreenUtil.getScreenWidthPix(mContext) * 0.3)));
		mProgressDialog.setCanceledOnTouchOutside(false);
		mProgressDialog.setCancelable(true);
		mProgressDialog.show();
	}

	// 取消扫描时的对话框
	public void dismissScanProgressDialog() {
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
			mProgressDialog = null;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		mCurrentPageID = mViewPager.getCurrentItem();
		if (mVideos == null) {
			mVideos = new ArrayList<MediaFile>();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
}
